package com.wangdao.wechatmall.service.admin;

import com.wangdao.wechatmall.bean.bo.admin.OrderBO;
import com.wangdao.wechatmall.bean.bo.admin.RefundBO;
import com.wangdao.wechatmall.bean.bo.admin.ShipBO;
import com.wangdao.wechatmall.bean.pojo.*;
import com.wangdao.wechatmall.bean.vo.admin.OrderDetailVO;
import com.wangdao.wechatmall.bean.vo.admin.OrderVO;
import com.wangdao.wechatmall.bean.vo.wx.WXUserIndexOrderVO;
import com.wangdao.wechatmall.bean.vo.wx.WXUserIndexVO;
import com.wangdao.wechatmall.mapper.OrderGoodsMapper;
import com.wangdao.wechatmall.mapper.OrderMapper;
import com.wangdao.wechatmall.mapper.UserMapper;
import com.wangdao.wechatmall.util.MyPageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author ZhangXiao on 2021/7/8
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Override
    public long getOrderNumber() {
        OrderExample orderExample = new OrderExample();
        long orderTotal = orderMapper.countByExample(orderExample);
        return orderTotal;
    }

    @Override
    public OrderVO queryList(OrderBO orderBO) {

        OrderVO orderVO = new OrderVO();
        MyPageHelper.startPage(orderBO);
        OrderExample orderExample = new OrderExample();

        OrderExample.Criteria criteria = orderExample.createCriteria();


        if (orderBO.getOrderStatusArray() != null){
            criteria.andOrderStatusIn(orderBO.getOrderStatusArray());


        }
        if (orderBO.getUserId() != null) {
            criteria.andUserIdEqualTo(orderBO.getUserId());
        }
        if (orderBO.getOrderSn() != null && !orderBO.getOrderSn().equals("")) {
            criteria.andOrderSnEqualTo(orderBO.getOrderSn());
        }
        orderExample.setOrderByClause(orderBO.getSort() + " " + orderBO.getOrder());
        List<Order> orders = orderMapper.selectByExample(orderExample);
        int total = (int) orderMapper.countByExample(orderExample);

        orderVO.setItems(orders);
        orderVO.setTotal(total);

        return orderVO;
    }

    @Override
    public OrderDetailVO showDetail(Integer id) {
        OrderDetailVO orderDetailVO = new OrderDetailVO();

        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(id);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
        orderDetailVO.setOrderGoods(orderGoods);

        Order order = orderMapper.selectByPrimaryKey(id);
        orderDetailVO.setOrder(order);


        User user = userMapper.selectByPrimaryKey(order.getUserId());
        OrderDetailVO.UserDTO userDTO = new OrderDetailVO.UserDTO();
        userDTO.setNickname(user.getNickname());
        userDTO.setAvatar(user.getAvatar());
        orderDetailVO.setUser(userDTO);

        return orderDetailVO;
    }

    @Override
    public int sendGoods(ShipBO shipBO) {
        Order order = new Order();
        order.setId(shipBO.getOrderId());
        order.setShipChannel(shipBO.getShipChannel());
        order.setShipSn(shipBO.getShipSn());
        order.setShipTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        order.setOrderStatus((short) 301);
        int update = orderMapper.updateByPrimaryKeySelective(order);
        return update;
    }

    @Override
    public int refundMoney(RefundBO refundBO) {
        Order order = new Order();
        order.setId(refundBO.getOrderId());
        order.setOrderStatus((short) 203);
        int update = orderMapper.updateByPrimaryKeySelective(order);
        return update;
    }

    @Override
    public WXUserIndexVO selectOrderStatus(int userId) {

        WXUserIndexVO userIndexVO = new WXUserIndexVO();
        WXUserIndexOrderVO order1 = new WXUserIndexOrderVO();
        List<Order> orders = orderMapper.queryOrderStatus(userId);

        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;
        for (Order order : orders) {
            Short orderStatus = order.getOrderStatus();
            if (orderStatus == 101) {
                count1 += 1;
                order1.setUnpaid(count1);
            } else if (orderStatus == 201) {
                count2 += 1;
                order1.setUnship(count2);
            } else if (orderStatus == 301) {
                count3 += 1;
                order1.setUnrecv(count3);
            } else if (orderStatus == 401) {
                count4 += 1;
                order1.setUncomment(count4);
            }
        }
        userIndexVO.setOrder(order1);
        return userIndexVO;
    }
}
